package com.kingsoft.dc.khaos.plugin.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Json处理工具类.
 */
public class JsonUtils {

    public static Map toMap(String json) {
        return JSON.parseObject(json, Map.class);
    }

    /**
     * 从map中提取List类型的值
     * @param map
     * @param tClass
     * @param keys
     * @param <T>
     * @return
     */
    public static <T> List<T> extractListValue(Map<String, Object> map, Class<T> tClass, String... keys) {
        List<String> keyList = Arrays.asList(keys);
        List<String> middleKeys = keyList.subList(0, keyList.size() - 1);
        String leafKey = keyList.get(keyList.size() - 1);
        for (String key : middleKeys) {
            map = JSON.parseObject(map.get(key).toString(), Map.class);
        }
        return JSONArray.parseArray(map.get(leafKey).toString(), tClass);
    }


    public static boolean containsKey(Map<String, Object> map, String... keys) {
        boolean ifContains = false;
        List<String> keyList = Arrays.asList(keys);
        if (keyList.size() == 1) {
            ifContains = map.containsKey(keyList.get(0));
        } else {
            List<String> middleKeys = keyList.subList(0, keyList.size() - 1);
            String leafKey = keyList.get(keyList.size() - 1);
            for (String key : middleKeys) {
                if (map.containsKey(key)) {
                    ifContains = true;
                    map = JSON.parseObject(map.get(key).toString(), Map.class);
                } else {
                    ifContains = false;
                    break;
                }
            }
            if (ifContains) {
                ifContains = map.containsKey(leafKey);
            }
        }
        return ifContains;

    }


    public static String extractStringValue(Map<String, Object> map, String... keys) {
        Object value = extractValue(map, keys);
        return value.toString();
    }

    public static Map extractMapValue(Map<String, Object> map, String... keys) {
        Object value = extractValue(map, keys);
        return JSON.parseObject(value.toString(), Map.class);
    }

    public static <T> T extractValue(Map<String, Object> map, String... keys) {
        List<String> keyList = Arrays.asList(keys);
        List<String> middleKeys = keyList.subList(0, keyList.size() - 1);
        String leafKey = keyList.get(keyList.size() - 1);
        for (String key : middleKeys) {
            map = JSON.parseObject(map.get(key).toString(), Map.class);
        }
        return (T)map.get(leafKey);
    }

    public static <T> T extractClassValue(Map<String, Object> map, Class<T> tClass, String... keys) {
        Object object = extractValue(map, keys);
        return JSON.parseObject(object.toString(), tClass);
    }

    public static <T> List<T> extractList(List<Map> _list, Class<T> tClass, String key) {
        List<T> tList = new ArrayList<>();
        for (Map<String, Object> map : _list) {
            tList.add((T)map.get(key));
        }
        return tList;
    }
}
